using iTextSharp.text;
using iTextSharp.text.pdf;
using VotingSystem.Models;

namespace VotingSystem.Services
{
    public class PdfExportService
    {
        public byte[] ExportSurveyToPdf(Survey survey)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Document document = new Document(PageSize.A4, 25, 25, 30, 30);
                PdfWriter writer = PdfWriter.GetInstance(document, ms);
                document.Open();

                // 使用iTextSharp自带的STSong-Light字体，支持中文
                BaseFont baseFont = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                Font titleFont = new Font(baseFont, 18, Font.BOLD);
                Font descFont = new Font(baseFont, 12);
                Font questionFont = new Font(baseFont, 14, Font.BOLD);
                Font optionFont = new Font(baseFont, 12);
                Font metaFont = new Font(baseFont, 10, Font.ITALIC);

                // 添加标题
                Paragraph title = new Paragraph(survey.Title, titleFont);
                title.Alignment = Element.ALIGN_CENTER;
                title.SpacingAfter = 20f;
                document.Add(title);

                // 添加描述
                if (!string.IsNullOrEmpty(survey.Description))
                {
                    Paragraph description = new Paragraph(survey.Description, descFont);
                    description.SpacingAfter = 20f;
                    document.Add(description);
                }

                // 添加问题
                int questionNumber = 1;
                foreach (var question in survey.Questions)
                {
                    // 添加题号和问题
                    Paragraph questionText = new Paragraph($"{questionNumber}. {question.QuestionText}", questionFont);
                    questionText.SpacingBefore = 15f;
                    questionText.SpacingAfter = 10f;
                    document.Add(questionText);

                    // 添加选项
                    if (question.Options != null)
                    {
                        foreach (var option in question.Options)
                        {
                            Paragraph optionText = new Paragraph($"• {option.OptionText}", optionFont);
                            optionText.IndentationLeft = 20f;
                            document.Add(optionText);
                        }
                    }
                    questionNumber++;
                }

                // 添加元数据
                Paragraph metadata = new Paragraph($"创建时间: {survey.CreatedAt:yyyy-MM-dd HH:mm}", metaFont);
                metadata.Alignment = Element.ALIGN_RIGHT;
                metadata.SpacingBefore = 30f;
                document.Add(metadata);

                document.Close();
                return ms.ToArray();
            }
        }

        public byte[] ExportVoteToPdf(Vote vote)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Document document = new Document(PageSize.A4, 25, 25, 30, 30);
                PdfWriter writer = PdfWriter.GetInstance(document, ms);
                document.Open();

                // 使用iTextSharp自带的STSong-Light字体，支持中文
                BaseFont baseFont = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                Font titleFont = new Font(baseFont, 18, Font.BOLD);
                Font descFont = new Font(baseFont, 12);
                Font optionFont = new Font(baseFont, 12);
                Font metaFont = new Font(baseFont, 10, Font.ITALIC);

                // 添加标题
                Paragraph title = new Paragraph(vote.Title, titleFont);
                title.Alignment = Element.ALIGN_CENTER;
                title.SpacingAfter = 20f;
                document.Add(title);

                // 添加描述
                if (!string.IsNullOrEmpty(vote.Description))
                {
                    Paragraph description = new Paragraph(vote.Description, descFont);
                    description.SpacingAfter = 20f;
                    document.Add(description);
                }

                // 添加选项
                foreach (var option in vote.Options)
                {
                    Paragraph optionText = new Paragraph($"• {option.Title}", optionFont);
                    optionText.SpacingBefore = 10f;
                    document.Add(optionText);
                }

                // 添加元数据
                Paragraph metadata = new Paragraph($"创建时间: {vote.CreateTime:yyyy-MM-dd HH:mm}", metaFont);
                metadata.Alignment = Element.ALIGN_RIGHT;
                metadata.SpacingBefore = 30f;
                document.Add(metadata);

                document.Close();
                return ms.ToArray();
            }
        }
    }
} 